package demo;

import java.sql.Date;
import java.util.ArrayList;
import java.util.GregorianCalendar;

import modelo.Alquiler;
import modelo.AlquilerPK;
import modelo.Genero;
import modelo.Pelicula;
import modelo.Promocion;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.zkoss.web.fn.ServletFns;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.SelectEvent;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Button;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Comboitem;
import org.zkoss.zul.Label;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import app.Facade;

public class FacadeZul extends SelectorComposer
{
	private Facade facade;
	
	@Wire("button#boton")
	Button btn;
	
	@Wire("combobox#cbox")
	Combobox combo;
	
	@Wire("combobox#cbox2")
	Combobox combo2;
	
	@Wire("combobox#cbox3")
	Combobox combo3;
	
	@Wire("textbox#id")
	Textbox txt1;
	
	@Wire("textbox#user")
	Textbox txt2;
	
	@Wire("textbox#cuponera")
	Textbox txt3;
	
	@Wire("textbox#dias")
	Textbox txt4;
	
	@Wire("label#lbl")
	Label lbl;
	
	@Wire("label#label")
	Label msjFinal;
	
	@Listen("onCreate = window#w")
	public void init1()
	{
		
		XmlBeanFactory factory=new XmlBeanFactory(new ClassPathResource("beans.xml"));
		facade=(Facade)factory.getBean("Facade");
		
		
		ArrayList<Genero> lst=(ArrayList<Genero>)facade.obtenerGeneros();
		//System.out.println(lst.size());
		int i = 0;
		for(Genero g:lst){
			
			//combo.getItems().add(e)
			combo.getItems().add(i,new Comboitem(g.getDescripcion()));
			i++;
			//combo.getItems().add(g.getIdGenero(),(Comboitem)Comboitem.this.setAttribute(g.getDescripcion(),g));
		}
		
		//System.out.println(combo.getSelectedIndex());
		
		
		
	}
	
	@Listen("onSelect = combobox#cbox")
	public void select(SelectEvent event) {
	    // called when onSelect is received on the component of id listbox0.
		//System.out.println(combo.getSelectedIndex());
		combo2.setVisible(true);
		mostrarPeliculas();
	}
	
	
	@Listen("onSelect = combobox#cbox2")
	public void select1(SelectEvent event) {
	    // called when onSelect is received on the component of id listbox0.
		//System.out.println(combo.getSelectedIndex());
		
		mostrarPromociones();
		
	}
	
	public void mostrarPeliculas(){
		combo2.getChildren().clear();
		
		//System.out.println(combo.getSelectedItem().getLabel());
			
		ArrayList<Pelicula> lst=(ArrayList<Pelicula>)facade.obtenerPeliculasXGenero(facade.obtenerIdGenero(combo.getSelectedItem().getLabel()));
		
		int i = 0;
		for(Pelicula p:lst){
			
			//combo.getItems().add(e)
			combo2.getItems().add(i,new Comboitem(p.getTitulo()));
			i++;
			//combo.getItems().add(g.getIdGenero(),(Comboitem)Comboitem.this.setAttribute(g.getDescripcion(),g));
		}
		
	}
	
	public void mostrarPromociones(){
		combo3.getItems().clear();                                                                                       
		ArrayList<Promocion> lst=(ArrayList<Promocion>)facade.obtenerPromociones();
		int i=0;
		for(Promocion p:lst){
			combo3.getItems().add(i, new Comboitem(p.getDescripcion()));
			i++;
		}
	}
	
	@Listen("onClick = button#boton")
	public void logIn(){
		int id=Integer.valueOf(txt1.getText());
		String user= txt2.getText();
		if(facade.identificarCliente(id,user)!= null)
			//System.out.println("bien");
			//lbl.setValue("sos una fiera "+user);
			alquilar();
		else
			lbl.setValue("Usuario equivocado");
	}
	
	public void alquilar(){
		AlquilerPK pk=new AlquilerPK();
		GregorianCalendar fecha=new GregorianCalendar();
		int idPelicula=facade.obtenerIdPelicula(combo2.getSelectedItem().getLabel());
		int idCliente=Integer.valueOf(txt1.getText());
		
		pk.setFecha(new Date(fecha.getTimeInMillis()));
		pk.setIdCliente(idCliente);
		pk.setIdPelicula(idPelicula);
		
		Alquiler alquiler= new Alquiler();
		alquiler.setIdAlquiler(pk);
		alquiler.setNroCupon(Integer.valueOf(txt3.getText()));
		alquiler.setDias(Integer.valueOf(txt4.getText()));
		alquiler.setPromocion(facade.traerPromocion(combo3.getSelectedItem().getLabel()));
		
		facade.agregarAlquiler(alquiler);
		
		msjFinal.setValue("Gracias por alquilar en El Gendarme");
		
	}
	
	

	
}
